package com.sam.expensetracker;

import com.sam.expensetracker.dao.ExpRec;
import com.sam.expensetracker.db.DBObject;

import net.rim.blackberry.api.homescreen.HomeScreen;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.ui.UiApplication;

/**
 * This class extends the UiApplication class, providing a graphical user
 * interface.
 */
public class MyApp extends UiApplication {

	/**
	 * Entry point for application
	 * 
	 * @param args
	 *            Command line arguments (not used)
	 */

	static public DBObject dbObject;
	static PersistentObject persistentObject;

	public static void main(String[] args) {
		// Create a new instance of the application and make the currently
		// running thread the application's event dispatch thread.
		MyApp theApp = new MyApp();
		theApp.enterEventDispatcher();
		
	}

	/**
	 * Creates a new MyApp object
	 */
	public MyApp() {
		// Push a screen onto the UI stack for rendering.
		pushScreen(new MyScreen());
	}

	public void deactivate() {

		synchronized (persistentObject) {
			persistentObject.setContents(dbObject);
			persistentObject.commit();
			
		}
		super.deactivate();
	}

	static {
			persistentObject = PersistentStore
					.getPersistentObject(0x8df938592c4d02cL);
			if (persistentObject != null) {
				synchronized (persistentObject) {
					if (persistentObject.getContents() == null) {
						persistentObject.setContents(new DBObject());
						persistentObject.commit();
					}
				}
				dbObject = new DBObject();
				dbObject = (DBObject) persistentObject.getContents();
			}
		
	}

}
